package org.csu.softwaremetrics_demo.util;

import org.apache.log4j.Logger;

/**
 * @Description: 带颜色的日志输出
 * @Author: Jukomu
 * @Package: org.example.util
 * @Project: Mybatis-demo
 * @name: ColorLogger
 * @Date: 2024/7/3-13:10
 * @Filename: ColorLoggerUtils
 */
public class ColorLoggerUtils {
    private static final String ANSI_RED = "\u001B[31m";
    private static final String ANSI_GREEN = "\u001B[32m";
    private static final String ANSI_YELLOW = "\u001B[33m";
    private static final String ANSI_BLUE = "\u001B[34m";
    private static final String ANSI_WHITE = "\u001B[37m";
    private static final String ANSI_PURPLE = "\u001B[35m";
    private static final String ANSI_COLOR_END = "\u001B[0m";
    private final String name;
    private final Logger logger;

    public ColorLoggerUtils(String name) {
        this.name = name;
        this.logger = Logger.getLogger(name);
    }

    public ColorLoggerUtils(Class clazz) {
        this.name = clazz.getName();
        this.logger = Logger.getLogger(name);
    }

    public static ColorLoggerUtils getLogger(String name) {
        return new ColorLoggerUtils(name);
    }

    public static ColorLoggerUtils getLogger(Class clazz) {
        return new ColorLoggerUtils(clazz);
    }

    protected Object getRedMessage(Object message) {
        message = ANSI_RED + message + ANSI_COLOR_END;
        return message;
    }

    protected Object getBlueMessage(Object message) {
        message = ANSI_BLUE + message + ANSI_COLOR_END;
        return message;
    }

    protected Object getYellowMessage(Object message) {
        message = ANSI_YELLOW + message + ANSI_COLOR_END;
        return message;
    }

    protected Object getGreenMessage(Object message) {
        message = ANSI_GREEN + message + ANSI_COLOR_END;
        return message;
    }

    protected Object getPurpleMessage(Object message) {
        message = ANSI_PURPLE + message + ANSI_COLOR_END;
        return message;
    }


    public void debug(Object message) {
        logger.debug(getBlueMessage("- [DEBUG] - " + message));
    }


    public void debug(Object message, Throwable t) {
        logger.debug(getBlueMessage("- [DEBUG] - " + message), t);
    }


    public void error(Object message) {
        logger.error(getRedMessage("- [ERROR] - " + message));
    }


    public void error(Object message, Throwable t) {
        logger.error(getRedMessage("- [ERROR] - " + message), t);
    }


    public void fatal(Object message) {
        logger.fatal(getRedMessage("- [FATAL] - " + message));
    }


    public void fatal(Object message, Throwable t) {
        logger.fatal(getRedMessage("- [FATAL] - " + message), t);
    }


    public void info(Object message) {
        logger.info(getGreenMessage("- [INFO] - " + message));
    }


    public void info(Object message, Throwable t) {
        logger.info(getGreenMessage("- [INFO] - " + message), t);
    }


    public void warn(Object message) {
        logger.warn(getYellowMessage("- [WARN] - " + message));
    }


    public void warn(Object message, Throwable t) {
        logger.warn(getYellowMessage("- [WARN] - " + message), t);
    }
}
